/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.prozezuak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import dBasea.Abokatua;
import dBasea.Bidaiariak;
import dBasea.Inplikatuak;
import dBasea.Istripu;

public class IstripuaSortu extends Panela {

	private static final long serialVersionUID = -7761607703140633726L;
	private JLabel helbidea;
	private JTextField helbi;
	private JLabel hiria;
	private JLabel oharrak;
	private JButton ok;
	private JButton ezeztatu;
	private JTextArea ohar;
	private JComboBox hiri;
	private JButton gehitu;
	private DefaultTableModel model;
	private JTable table;
	private Abokatua abok = new Abokatua();
	private Istripu istri = new Istripu();
	private Bidaiariak bida = new Bidaiariak();
	private Inplikatuak inpli = new Inplikatuak();

	public IstripuaSortu(String titulo, final ItzuliInterfazea itzuliInterfazea) {
		super(titulo);

		JPanel erdikoPanela = this.getErdikoPanela();
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		// Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel erdiko = new JPanel();
		erdiko.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdiikk = new FlowLayout();
		fl_erdiikk.setHgap(15);
		fl_erdiikk.setVgap(8);
		erdiko.setLayout(fl_erdiikk);
		helbidea = new JLabel("Helbidea:");
		helbidea.setFont(new Font("Arial", Font.PLAIN, 14));
		helbi = new JTextField(14);
		erdiko.add(helbidea);
		erdiko.add(helbi);
		erdikoa.add(erdiko);

		JPanel erdikox = new JPanel();
		erdikox.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdiiko = new FlowLayout();
		fl_erdiiko.setHgap(15);
		fl_erdiiko.setVgap(8);
		erdikox.setLayout(fl_erdiiko);
		hiria = new JLabel("Hiria:");
		hiria.setVerticalAlignment(SwingConstants.TOP);
		hiria.setHorizontalAlignment(SwingConstants.CENTER);
		hiria.setFont(new Font("Arial", Font.PLAIN, 14));
		hiri = new JComboBox();
		try {
			boxCarga();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					"Ezin izan da kargatu DB-tik hiri zerrenda", "Oker",
					JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		erdikox.add(hiria);
		erdikox.add(hiri);
		erdikoa.add(erdikox);

		JPanel erdikoaa = new JPanel();
		erdikoaa.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdiik = new FlowLayout();
		fl_erdiikk.setHgap(15);
		fl_erdiikk.setVgap(8);
		erdikoaa.setLayout(fl_erdiik);
		oharrak = new JLabel("Oharrak:");
		oharrak.setFont(new Font("Arial", Font.PLAIN, 14));
		ohar = new JTextArea();
		ohar.setRows(2);
		ohar.setColumns(25);
		erdikoaa.add(oharrak);
		erdikoaa.add(ohar);
		erdikoa.add(erdikoaa);

		gehitu = new JButton("Gehitu bidaiari NAN");
		JPanel aux = new JPanel();
		aux.setBackground(SystemColor.activeCaption);
		aux.setLayout(new FlowLayout());

		model = new DefaultTableModel();
		model.addColumn("Bidaiaria");
		model.addColumn("Matrikula");

		table = new JTable(model);
		table.setPreferredSize(new Dimension(400, 50));
		table.setPreferredScrollableViewportSize(new Dimension(400, 50));
		JScrollPane scrollPane = new JScrollPane(table);

		gehitu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new BidaiariaSartuPane(IstripuaSortu.this);
			}
		});

		JPanel pnal = new JPanel(new FlowLayout());
		pnal.setBackground(SystemColor.activeCaption);
		pnal.add(gehitu);
		erdikoa.add(pnal);

		aux.add(scrollPane, BorderLayout.CENTER);
		erdikoa.add(aux);

		// beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Istripua sortu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!zerbaiIdatzi()) {
					JOptionPane.showMessageDialog(null,
							"Sartu behar duzu zerbait zuriunetan\n"
									+ "Ezin da eremu hutsik egon", "Oker",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						String helbidea = helbi.getText();
						String oharrak = ohar.getText();
						String hiria = (String) hiri.getSelectedItem();

						int istripuaId = istri.istriSortu(helbidea, hiria,
								oharrak);

						for (int i = 1; i <= model.getRowCount(); i++) {
							String nan = (String) model.getValueAt(i - 1, 0);
							String matrikula = (String) model.getValueAt(i - 1,
									1);

							if (!bida.dagoBidaiaria(nan)) {
								// existitzen ez bada
								// bidaiaria sartu
								// BIDAIARIA
								bida.sartuBidaiaria(nan);// sartzeko metodoa
							}
							if (!inpli.dagoMatrikula(matrikula)) {
								// existitzen ez
								// bada
								// matrikula sartu
								// Matrikula
								// IbilgailuInplikatuan
								inpli.sartuMatrikula(matrikula);// sartu
																// matrikula
							}

							istri.inbolukratuakSortu(istripuaId, nan, matrikula);

						}
						eremuakGarbitu();
						JOptionPane.showMessageDialog(null,
								"Istripua sortu da", "Ondo",
								JOptionPane.INFORMATION_MESSAGE);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(null,
								"DBan oker gertatu da", "Oker",
								JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
	}

	private void boxCarga() throws SQLException {
		List<String> lista = abok.getHiriak();
		hiri.removeAllItems();
		if (lista != null) {
			for (String hiria : lista) {
				hiri.addItem(hiria);
			}
		}
	}

	@Override
	public void eremuakGarbitu() {
		helbi.setText("");
		ohar.setText("");
		hiri.setSelectedIndex(0);
		for (int i = model.getRowCount() - 1; i >= 0; i--) {
			model.removeRow(i);
		}

	}

	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki = helbi.getText().length() > 0 && ohar.getText().length() > 0;
		return aurki;
	}

	// BidaiariaSaturPane JDialog soilik
	public void addNewRow(String nan, String matrikula) {
		model.addRow(new Object[] { nan, matrikula });
	}
}